<<<<<<< HEAD
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Borrow extends MY_Controller {
		
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /sys_admin/login');
			exit;
		}
		
		$this->tab = 'borrowtender';
	}	
	
	/**
	 * 
	 * 计算投资周期
	 */
	function billtimes(){
		$ret = array(
			'msg' => '',
			'info' => '',
		);
		if(intval($_POST['billtimeday'])>0){
			$billtimes=intval($_POST['billtimeday']);
			$time=strtotime($_POST['bill_starttime'])+$billtimes*24*3600;
			//****** 20141222 by liub 
			$repaymenttimes=date('Y-m-d H:i',$time);
			$ret['retinfo'] = $repaymenttimes;
			$ret['info'] = $billtimes;
		}else{
			// 以自然月计算最后还款日期
			$billtimes=intval($_POST['billtime']);
			$time = strtotime($_POST['bill_starttime']);
			$repaymenttimes=date('Y-m-d H:i',$time);
			$repay_time = strtotime($repaymenttimes);
 			$last_repay_time = get_times(array("time"=>$repay_time,"num"=>$billtimes,"type"=>'month'));
 			$last_repay_date=date('Y-m-d H:i',$last_repay_time);
			$ret['retinfo'] = $last_repay_date;
			$ret['info'] = $billtimes*30;
		}		
		echo json_encode($ret);
		exit;
	}
	
	
	/**
	 * 
	 * 公共的标列表数据
	 * @param int $fstatus 
	 * @param varchar $sub 
	 * @param int $show
	 * @param varchar $func
	 */
	private function common_borrow_list($fstatus,$sub,$show,$func){
    	$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
    	$title 				= $this->input->get('title');
    	$work_time 			= $this->input->get('work_time');
    	$style 				= $this->input->get('style');
    	$search_fstatus 	= $this->input->get('fstatus')?$this->input->get('fstatus'):-2;
		
		if($title != ''){
			$data['where']['title'] = $title;
		}
		if($work_time != 0){
			$data['where']['work_time'] = $work_time;
		}
		if($style != 0){
			$data['where']['style'] = $style;
		}
		$data['page_size']  = 10;
		$data['page_start'] = (int)$per_page;
		if($fstatus == 'full'){
			$data['where']['qtype'] = $fstatus;
		}else if($fstatus == 'passing'){
			$data['where']['qtype'] = $fstatus;
		}else if($fstatus == 'refunding'){
			$data['where']['qtype'] = $fstatus;
		}else{
			if($search_fstatus != -2){
				$data['where']['fstatus'] = $search_fstatus;
			}else{
				if($fstatus > 0){
					$data['where']['fstatus'] = $fstatus;
				}	
			}
		}
		$vdata = $this->common_borrow_conset();	
		$data['select'] 	= 1;
		$this->load->model('Borrow_model');
		$borrow_list = $this->Borrow_model->get_borrow_user_list($data);
		$vdata['borrow_list'] = $borrow_list['borrow_list'];
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/borrow/'.$func.'/?fstatus='.$search_fstatus.'&title='.$title.'&work_time='.$work_time.'&style='.$style;
		$config['total_rows'] = $borrow_list['borrow_count'];
		$config['per_page'] = sys_admin_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
						
		$vdata['tab'] = $this->tab;		
    	$vdata['sub'] = $sub;
		$vdata['show'] = $show;
		$vdata['fstatus'] = $search_fstatus;
		$vdata['title'] = $title;
		$vdata['work_time'] = $work_time;
		$vdata['style'] = $style;
		$vdata['func'] = $func;
		$this->load->view('/sys_admin/borrow/borrow_list', $vdata);		
    }
    
    
    /**
     * 
     * 共用的常量
     */
    private function common_borrow_conset(){
    	$this->load->model('Borrow_model');
    	$vdata['borrow_type'] 				= $this->Borrow_model->arrays('');//标产品
		$vdata['borrow_work_time'] 			= $this->Borrow_model->arrays('borrow_work_time');//计息方式
		$vdata['borrow_style'] 				= $this->Borrow_model->arrays('borrow_style');//还款方式
		$vdata['borrow_type'] 				= $this->Borrow_model->arrays('borrow_type');//还款方式
		$vdata['borrow_status'] 			= $this->Borrow_model->arrays('borrow_status');//状态
		$vdata['borrow_award'] 				= $this->Borrow_model->arrays('borrow_award');//客户奖励
		$vdata['borrow_good'] 				= $this->Borrow_model->arrays('borrow_good');
		$vdata['borrow_bill_bank'] 			= $this->Borrow_model->arrays('borrow_bill_bank');
		$vdata['borrow_time_limit'] 		= $this->Borrow_model->arrays('borrow_time_limit');//借款期限
		$vdata['borrow_valid_time'] 		= $this->Borrow_model->arrays('borrow_valid_time');//投资有效期
		$vdata['borrow_day_time_limit'] 	= $this->Borrow_model->arrays('borrow_day_time_limit');//借款期限
		$vdata['borrow_verify'] 			= $this->Borrow_model->arrays('borrow_verify');//是否初检
		$vdata['borrow_check'] 				= $this->Borrow_model->arrays('borrow_check');//是否复检
		
		$this->load->model('Conset_model');
		//标的安全等级
		$vdata['borrow_fsafe'] 				= $this->Conset_model->get_credit_title_list();	
		//标的资金用途
		$vdata['borrow_fuse'] 				= $this->Conset_model->get_use_title_list();	
		//背景调查审核
		$vdata['borrow_fsubject'] 			= $this->Conset_model->get_subject_title_list();
		
		$vdata['conset_subject_valid'] = $this->Conset_model->arrays('conset_subject_valid');
		$vdata['conset_subject_file'] = $this->Conset_model->arrays('conset_subject_file');
		return $vdata;
		
    }
    
	private function common_borrow_obj($borrow_id,$sub){
		$data['select'] = 1;
		$this->load->model('Borrow_model');
		$borrow = $this->Borrow_model->get_borrow_obj($borrow_id, $data);
		$vdata = $this->common_borrow_conset();
		$vdata['borrow'] 					= $borrow;
		$this->load->model('Conset_model');
		if($borrow['fsubject']){
			$vdata['list'] = unserialize($borrow['fsubject']);
			$tmp = array();
			foreach($vdata['list'] as $v){
				$tmp[] =  $v['id'];
			}
			$vdata['sublist'] = $tmp;
		}
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = $sub;
		$vdata['fshow'] = $sub;
		return $vdata;
	}
	
	
	/***/
	private function upload_borrow_all($path,$file_upload){
		if(!file_exists($path)){
			mkdir($path,0777);
		}
		$config['upload_path'] = $path;
		$config['allowed_types'] = 'jpg|png|doc|docx|wps';
		$config['file_name'] = 'borrow_'.uniqid();
		$config['overwrite'] = true;
		$config['max_size'] = '512';
		$config['max_width']  = '2048';
		$config['max_height']  = '3000';
		$this->load->library('upload', $config);
		$thumbnail = Array();
		for($i = 0; $i < 2;$i++){
			if($this->upload->do_upload($file_upload[$i])){
				$tmp = $this->upload->data();
				//echo 
				$thumbnail[$i] = $path.$config['file_name'].$tmp['file_ext'];
				// 添加水印				
				$this->load->library('image_lib');		
	            $warter['source_image'] = $thumbnail[$i];
				$warter['wm_type'] = 'overlay';
				
				//******  by liub  20141211 修改水印图片，以及水印位置为 底部居右
				$warter['wm_overlay_path'] = WM_OVERLAY_FILE_PNG;
				$warter['wm_opacity'] = '60';
				$warter['wm_vrt_alignment'] = 'B';// 底部
				$warter['wm_hor_alignment'] = 'R';// 右边
				// ****************** end 
				
				$this->image_lib->initialize($warter);
				$this->image_lib->watermark();
			}else{
				echo "12345____".$i;exit;
			}
		}
		return $thumbnail;
	}
	
	
	public function upload_view(){
		$this->load->model('Conset_model');
		$vdata['conset_subject_valid'] = $this->Conset_model->arrays('conset_subject_valid');
		$vdata['conset_subject_file'] = $this->Conset_model->arrays('conset_subject_file');
		
		$vdata['sid'] = $_POST['sid'];
		$response_html = $this->load->view('/sys_admin/borrow/borrow_upload',$vdata,true);
		echo $response_html;
	}
	
	public function doupload(){
		$ret = array(
			'msg' => '',
			'url' => ''
		);
		
		$file_format = $_POST['fformat'];
		
		$sid =  $_POST['sid'];
		$this->load->model('Conset_model');
		$conset_subject_file = $this->Conset_model->arrays('conset_subject_file');
		
		
		$thumbnail = "";
		if(isset($_FILES['thumbnail']) && $_FILES['thumbnail']['name'] != ''){
			
			$thumbnail = $this->attach(UPFILES_CONSET_SUBJECT_IMG,$_FILES['thumbnail']['tmp_name'],$_FILES['thumbnail']['name'],$file_format );
			if ($thumbnail){
				$fileinfo = '';
				$thumbnailstr = '';
				
				$content=$_POST['subjectcon'];
				$valid=$_POST['subjectvalid'];
				
				$thumbnailstr .= $sid.'|'.$thumbnail.'|'.$file_format.'|'.$valid.'|'.$content;
				
				if($file_format == 1){
					$fileinfo .= '<li><a target="_blank" href="'.CONSET_SUBJECT_IMG.$thumbnail.'"><img src="'.CONSET_SUBJECT_IMG.$thumbnail.'" width="60" height="60"  /></a><br />'.$thumbnail.'<br />'.$conset_subject_file[$file_format].'<br />审核资料有效期:'.$valid.'个月<br /><p>资料提交说明：'.$content.'</p><input type="hidden" name="thumbnailstr[]" value="'.$thumbnailstr.'"/></li>';
				}else{
					$fileinfo .= '<li>'.$thumbnail.'<br />'.$conset_subject_file[$file_format].'<br />审核资料有效期:'.$valid.'个月<br /><p>资料提交说明：'.$content.'</p><input type="hidden" name="thumbnailstr[]" value="'.$thumbnailstr.'"/></li>';
				}				
					
				$ret['msg'] = array('thumbnail'=>$thumbnailstr,'info'=>$fileinfo);
				echo json_encode($ret);
				exit;
		
			}else{
				$ret['url'] = 'error';
				$ret['msg'] = '保存失败';
			}
		}else{
			$ret['url'] = 'error';
			$ret['msg'] = $this->upload->display_errors();
		}
		echo json_encode($ret);
		exit;
		
		
	}
	
	
	/**
	 * 添加标的标视图
	 */
	
	function zhs_publish_borrow(){
		$vdata = $this->common_borrow_conset();
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'zhs_publish_borrow';
		$this->load->view('/sys_admin/borrow/publish_borrow', $vdata);
	}
	
	/**
	 * 添加标的标
	 */
	function add(){

		// 前端页面获取数据
		$data = $this->post_data();

		//$data['user_id'] = ADMIN_ID; //平台自发	

  		$this->load->model('Borrow_model');
		$borrow_id = $this->Borrow_model->add($data);
		if($borrow_id == 0){
			$ret['msg'] = "发标失败。";
			echo json_encode($ret);exit;
		}
		$ret['msg'] = $borrow_id;
		$this->load->model('log_model');
		$this->log_model->add_log('[添加][标的标][id=='.$borrow_id.']');
		echo json_encode($ret);
	}
	
	/**
	 * 编辑标的标视图
	 * @param int $borrow_id
	 */
	function edit_view($borrow_id){
		$vdata = $this->common_borrow_obj($borrow_id, 'first');
		$this->load->view('/sys_admin/borrow/borrow_edit', $vdata);
	}
	
	/**
	 * 编辑标的标
	 */
	function edit(){
		$id = intval($this->input->post('id'));
		
		$data = $this->post_data();
  		$this->load->model('Borrow_model');
		$falg = $this->Borrow_model->update($id, $data);
		
		$ret['msg'] = $falg;
		$this->load->model('log_model');$this->log_model->add_log('[修改][标的标][ID=='.$id.']');
		echo json_encode($ret);
	}
	
	
	/**
	 * 标列表
	 */
	function zhs_borrowlist(){
		$this->common_borrow_list(-1,'all','all', 'zhs_borrowlist');	
	}
	
	private function borrow_verify_data($sub,$func){
    	$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
    	$title 				= $this->input->get('title');
    	$work_time 				= $this->input->get('work_time');
    	$style 				= $this->input->get('style');
    	$search_fstatus 			= $this->input->get('fstatus')?$this->input->get('fstatus'):-2;
		if($title != ''){
			$data['where']['title'] = $title;
		}
		if($work_time != 0){
			$data['where']['work_time'] = $work_time;
		}
		if($style != 0){
			$data['where']['style'] = $style;
		}
		$data['page_size']  = sys_admin_LIST_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		if($sub == 'first'){//初检
			$data['where']['qtype'] = $sub;
		}else if($sub == 'again'){//复检
			$data['where']['qtype'] = $sub;
		}else if($sub == 'publish'){//发布
			$data['where']['qtype'] = $sub;
		}
		
		$vdata = $this->common_borrow_conset();	
		
		$data['select'] 	= 1;
		$this->load->model('Borrow_model');
		$borrow_list = $this->Borrow_model->get_borrow_user_list($data);
		$vdata['borrow_list'] = $borrow_list['borrow_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/borrow/'.$func.'/?';
		$config['total_rows'] = $borrow_list['borrow_count'];
		$config['per_page'] = sys_admin_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
									
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = $sub;
		$vdata['fshow'] = $sub;
		
		$this->load->view('/sys_admin/borrow/borrow_verify_list', $vdata);
	}
	
	/**
	 * 
	 *待复检中投标产品列表
	 */
	function zhs_borrow_verify_again_list(){
		$this->borrow_verify_data('again','zhs_borrow_verify_again_list');	
	}
	
	 /**
	 * 
	 * 待初检中的投标产品列表
	 */
    function zhs_borrow_verify_list(){
    	$this->borrow_verify_data('first','zhs_borrow_verify_list');			
    }
    
	 /**
	 * 
	 * 待发布的投标产品列表
	 */
    function zhs_borrow_publish_list(){
    	$this->borrow_verify_data('publish','zhs_borrow_publish_list');			
    }
	    
     /**
	 * 
	 * 借款中的投标产品列表
	 */
    function zhs_borrow_going_list(){
    	$this->common_borrow_list(1, 'now','going','zhs_borrow_going_list');    	
    }
    
    /**
	 * 
	 * 满标待审标产品列表
	 */
    function zhs_borrow_full_list(){
    	$this->common_borrow_list('full','full','full', 'zhs_borrow_full_list');
    }
    
	/**
	 * 
	 * 还款中标产品列表
	 */
    function zhs_borrow_repayment_list(){    	
    	$this->common_borrow_list('refunding','repayment','repayment','zhs_borrow_repayment_list');
    }
    
    /**
	 * 
	 * 过往标产品列表
	 */
    function zhs_borrow_passing_list(){    	
    	$this->common_borrow_list('passing','passing','passing','zhs_borrow_passing_list');
    }
    
    
	/**
	 * 
	 * 投标详细记录
	 */
	function borrow_view($borrow_id){
		$this->load->model('Borrow_model');
		$this->load->model('User_model');
		
		$vdata = $this->common_borrow_obj($borrow_id, 'all');
		
		//投标记录信息
		$borrow_tender_list 			= $this->Borrow_model->get_borrow_tender(array('select'=>1,'where'=>array('borrow_id'=>$borrow_id)));
		$borrow_tender_list1 = array();
		foreach ($borrow_tender_list as $tender){
			$user_obj = $this->User_model->get_by_uid($tender['user_id']);
			$tender['username'] = $user_obj['username'];
			$tender['realname'] = $user_obj['realname'];
			$borrow_tender_list1[] 		= $tender;
		}
		$vdata['borrow_tender_list'] = $borrow_tender_list1;			
	
		$this->load->view('/sys_admin/borrow/borrow_view', $vdata);
	}
	
	/**
	 * 撤标
	 */
	function withdraw($borrow_id){
		//接收参数
		$data = array('fstatus'=>2);
		$this->load->model('Borrow_model');
		$borrow_obj = $this->Borrow_model->get_borrow_obj($borrow_id, $data);
		
		$user = $this->session->userdata('rsh_user');
		$data['id'] 		= $borrow_id;
		$data['user_id'] 	= $borrow_obj['user_id'];
		$data['ip'] 		= $this->js->get_ip();
		$this->load->model('log_model');$this->log_model->add_log('[撤标][借款标]');
		echo $this->Borrow_model->Cancel($data);
	}
	
	
	
	
	function repayment(){
		//接收参数
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$strtime			= $this->input->get('strtime');
		$endtime			= $this->input->get('endtime');
		
		$data = array();		
		
		$data['where']['fstatus'] = 0;
		
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		
		$data['page_size']  = sys_admin_LIST_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$data['select'] 	= 1;
		$this->load->model('User_model');
		$this->load->model('Borrow_model');
		
		$vdata = $this->common_borrow_conset();
		$repayment_list  = $this->Borrow_model->get_repayment_list($data);
		
		$vdata['borrow_list'] = $repayment_list['repayment_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/borrow/repayment/?';
		$config['total_rows'] = $vdata['repayment_count'];
		$config['per_page'] = sys_admin_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		$vdata['strtime'] 	= $strtime;
		$vdata['endtime'] 	= $endtime;
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'repayment';
		$vdata['show'] = 'repayment';
		$this->load->view('/sys_admin/borrow/borrow_list', $vdata);
	}
	
	function repaymentexcel(){
		$title 				= $this->input->get('title');
		$strtime			= $this->input->get('strtime');
		$endtime			= $this->input->get('endtime');
		
		$data = array();
		/**/
		//TODO ***********************************
		if($fstatus != -1){
			$data['where']['fstatus'] = $fstatus;
		}
		if($title != ''){
			$data['where']['title'] = $title;
		}
		
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		
		$data['select'] 	= 1;
		$this->load->model('Borrow_model');
		$this->load->model('User_model');
		$vdata = $this->Borrow_model->get_repayment_list($data);
		$repayment_list = $vdata['repayment_list'];
		$repayment_list_new = array();
		foreach($repayment_list as $repayment){
			$user_obj = $this->User_model->get_by_uid($repayment['user_id']);
			$repayment['username'] = $user_obj['username'];
			$repayment['repayment_account_majia'] = $this->Borrow_model->get_borrow_collection_majia(array('borrow_id'=>$repayment['borrow_id'],'forder'=>$repayment['forder']));
			$repayment_list_new[] = $repayment;
		}
		$vdata['repayment_list'] = $repayment_list_new;
		
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory'); 
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle("还款列表")->setDescription(COMPANY_NAME); 
		$objPHPExcel->setActiveSheetIndex(0);
		$titles = array("编号", "借款户", "借款标题", "期数", "到期时间", "还款金额", "本金", "利息","真实金额", "马甲金额", "还款时间");
		
		for($i = 0; $i < count($titles); $i ++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
		}
		$repayment_list = $vdata['repayment_list'];
		for($i = 0; $i < count($repayment_list); $i++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $repayment_list[$i]['id']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $repayment_list[$i]['username']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, $repayment_list[$i]['title']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, ($repayment_list[$i]['forder']+1).'/'.$repayment_list[$i]['time_limit']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, date('Y-m-d H:i:s', $repayment_list[$i]['repayment_time']));
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i+2, $repayment_list[$i]['repayment_account']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $i+2, $repayment_list[$i]['repayment_account']-$repayment_list[$i]['repayment_account_majia']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $i+2, $repayment_list[$i]['repayment_account_majia']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8, $i+2, $repayment_list[$i]['capital'].' ');
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(9, $i+2, $repayment_list[$i]['interest']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(10, $i+2, date('Y-m-d H:i:s', $repayment_list[$i]['repayment_yestime']));
		}
		
		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
		//发送标题强制用户下载文件 
		header('Content-Type: application/vnd.ms-excel'); 
		header('Content-Disposition: attachment;filename="还款列表.xls"'); 
		header('Cache-Control: max-age=0'); 
		$objWriter->save('php://output'); 
	}
	
	/**
	 * 初检
	 */
	function verify_view($borrow_id){
		$vdata = $this->common_borrow_obj($borrow_id, 'first');
		$this->load->view('/sys_admin/borrow/borrow_verify', $vdata);
	}
	/**
	 * 复检
	 */
	function verify_again_view($borrow_id){
		$vdata = $this->common_borrow_obj($borrow_id, 'again');
		$this->load->view('/sys_admin/borrow/borrow_verify', $vdata);
	}
	

	/**
	 * 审标
	 */
	function verify(){
		$id 			= $this->input->post('id');
		$fstatus 		= $this->input->post('fstatus');
		$verify_remark 	= $this->input->post('verify_remark');
		
		$this->load->model('Borrow_model');
  		$borrow = $this->Borrow_model->get_borrow_obj($id);
  		if($borrow['fstatus'] != 0){
  			echo "该标不处于待审核状态";
  			return;
  		}
  		
  		$data["fstatus"]			= $fstatus;
  		$data["verify_remark"]		= $verify_remark;
  		$data["verify_time"]		= time();
  		
  		$admin_user = $this->session->userdata('admin_user');
  		$data["verify_user"]		= $admin_user['user_id'];
  		
		$flag = $this->Borrow_model->verify($id, $data);
		
		$this->log->write_log('info', $flag);
		if($flag == 1 && $fstatus == 1){//自动投标
			$this->Borrow_model->auto_tender_borrow($id);
		}
		$this->load->model('log_model');$this->log_model->add_log('[审核][发标]');
		echo $flag;
	}
	
	/**
	 * 进入复审
	 */
	function review_view($borrow_id){
		$vdata = $this->common_borrow_obj($borrow_id, 'full');
		$this->load->view('/sys_admin/borrow/borrow_review', $vdata);
	}
	
	
	/**
	 * 确认复审
	 */
	function review(){
		$id 				= $this->input->post('id');
		$fstatus 			= $this->input->post('fstatus');
		$repayment_remark 	= $this->input->post('repayment_remark');
		
  		$data['id']					= $id;
  		$data['ip']					= $this->js->get_ip();
  		$data["fstatus"]			= $fstatus;
  		$data["repayment_remark"]	= $repayment_remark;
  		
  		// TODO by liub 20150203
  		// $data["repayment_user"]		= ADMIN_ID;// 还款人
  		$this->load->model('Borrow_model');
  		$flag = 0;
  		if($fstatus == 3){//满标复审通过
  			$flag = $this->Borrow_model->zhs_borrow_review_true($data);
  		}elseif ($fstatus == 4){//复审不通过
  			//$flag = $this->Borrow_model->borrow_review_false($data);
  		}
  		$this->load->model('log_model');
  		$this->log_model->add_log('[复审][满标]');
  		
		echo $flag;
	}
	
	
	
	
	function repay($repayment_id, $user_id){
		$data['repayment_id'] 	= $repayment_id;
		$data['user_id'] 		= $user_id;
		$data['ip']	 			= $this->js->get_ip();
		$this->load->model('Borrow_model');
		echo $this->Borrow_model->repay($data);
		
	}
	
	/**
	 * 回款排行
	 */
	function repayment_top(){
		$strtime 			= $this->input->get('strtime');
		$endtime 			= $this->input->get('endtime');
		$username 			= $this->input->get('username');
		$data['where']['strtime']	= $strtime;
		$data['where']['endtime']	= $endtime;
		$data['where']['username']	= $username;
		$this->load->model('Borrow_model');
		$vdata['repayment_list'] = $this->Borrow_model->repayment_top($data);
		$vdata['strtime'] = $strtime;
		$vdata['endtime'] = $endtime;
		$vdata['username'] = $username;
		$this->load->view('/sys_admin/borrow/repayment_top', $vdata);
	}
	
	/**
	 * 网站统计
	 */
	function index(){
		$this->load->model('Borrow_model');		
		$data['total_data'] = $this->Borrow_model->admin_total_index();
		
		$data['tab'] = $this->tab;
		$data['sub'] = 'month';
		
		$this->load->view('/sys_admin/borrow/borrow_index', $data);
	}
	
	private function tender_data($limit=false){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$username 			= $this->input->get('username');
		$is_self 	= isset($_GET['is_self']) ? $this->input->get('is_self') : -1;
		$fstatus = isset($_GET['fstatus']) ? $this->input->get('fstatus') : -1;
		$strtime 			= $this->input->get('strtime');
		$endtime 			= $this->input->get('endtime');
		
		$title 			= $this->input->get('title');
		$time_limit 			= $this->input->get('time_limit');
		
		if($title != ''){
			$data['where']['title'] = $title;
		}
		
		if($time_limit != '' && $time_limit != -1){
			$data['where']['time_limit'] = $time_limit;
		}
		
		if($username != ''){
			$data['where']['username'] = $username;
		}
		
		if($is_self != '' && $is_self != -1){
			$data['where']['is_self'] = $is_self;
		}
		
		if($fstatus != '' && $fstatus != -1){
			$data['where']['fstatus'] = $fstatus;
		}
		
		
		$data['where']['strtime']	= $strtime;
		$data['where']['endtime']	= $endtime;
		$data['page_size']  = sys_admin_LIST_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$data['limit'] = $limit;
		
		$this->load->model('Borrow_model');
		$vdata 			  = $this->Borrow_model->borrow_tender_user_list($data);
		$tender_list = $vdata['tender_list'];
		$tender_user_id = array();
		foreach ($tender_list as $tender){
			$first_tender_obj = $this->Borrow_model->user_first_tender($tender['user_id']);
			if($first_tender_obj['id'] == $tender['id']){
				$tender['flag'] = 1;
			}			
			$tender_list1[] = $tender;
		}
		$vdata['tender_list'] = $tender_list1;
		$vdata['user_self'] 	= $this->Borrow_model->arrays("user_self");
		$vdata['borrow_tender_status'] 	= $this->Borrow_model->arrays("borrow_tender_status");
		$vdata['borrow_time_limit'] 	= $this->Borrow_model->arrays("borrow_time_limit");
		
		$vdata['username'] = $username;
		$vdata['is_self'] 	= $is_self;
		$vdata['fstatus'] = $fstatus;
		$vdata['strtime'] = $strtime;
		$vdata['endtime'] = $endtime;
		$vdata['title'] = $title;
		$vdata['time_limit'] = $time_limit;
		
		$vdata['user_count'] = $vdata['tender_user_count'];
		$vdata['tender_money'] =$vdata['tender_money_count'];
		
		return $vdata;
	}
	
	function tenderlist(){
		
		$vdata = $this->tender_data();
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/borrow/tenderlist/?strtime='.$vdata['strtime'].'&endtime='.$vdata['endtime'].'&username='.$vdata['username'].'&fstatus='.$vdata['fstatus'];
		$config['total_rows'] = $vdata['tender_count'];
		$config['per_page'] = sys_admin_LIST_PAGE_SIZE;
		$this->pagination->initialize($config);
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'tender';
		
		$this->load->view('/sys_admin/borrow/tender_list', $vdata);
	}
	
	/**
	 * 流转标撤销投标
	 */
	function cancel_tender(){
		$tid = intval($this->input->post('tid'));
		$this->load->model('Borrow_model');
		$msg = $this->Borrow_model->tender_cancel($tid);
		echo $msg;
		exit;
	}
	
	function tenderlistexcel(){
		
		$vdata = $this->tender_data('all');
		
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory'); 
		
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle("投标记录")->setDescription(COMPANY_NAME); 
		
		$objPHPExcel->setActiveSheetIndex(0);
		$titles = array("用户编号", "投资客", "借款标", "投标金额", "期限", "投标时间");
		
		for($i = 0; $i < count($titles); $i ++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
		}
				
		$tender_list = $vdata['tender_list'];
		
		for($i = 0; $i < count($tender_list); $i++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $tender_list[$i]['id']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $tender_list[$i]['username']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, $tender_list[$i]['title']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, number_format($tender_list[$i]['account'],2));
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, $tender_list[$i]['time_limit']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i+2, date('Y-m-d H:i:s', $tender_list[$i]['add_time']));
		}
		
		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
		header('Content-Type: application/vnd.ms-excel'); 
		header('Content-Disposition: attachment;filename="投标记录.xls"'); 
		header('Cache-Control: max-age=0'); 
		$objWriter->save('php://output'); 
		exit;
	}
	
	
	function borrow_account(){
		$this->load->model('Borrow_model');
		$post_data = $this->input->post();
		$id = intval($post_data['id']);
		$lowest = intval($post_data['lowest_account']);
		$most = intval($post_data['most_account']);
		$borrow_info = $this->Borrow_model->get_borrow_obj($id);
		if($lowest<100){
			echo "最低投标金额不能小于100元";
			exit;
		}else if($most != 0 && $lowest>$most){
			echo "最低投标金额不能大于最高投标金额";
			exit;
		}else if($most != 0 && $most>$borrow_info['account']){
			echo "最高投标金额不能大于借贷总金额";
			exit;
		}
		
		$ret = $this->Borrow_model->update($id,array('lowest_account'=>$lowest,'most_account'=>$most));
		
		echo $ret ? '操作成功' : '操作失败';
	}
	
	
	
	function borrow_single($id){
		$this->load->model('Borrow_model');
		$data['id']	= $id;
  		$data['ip']	= $this->js->get_ip();
		echo $this->Borrow_model->borrow_review_single_false($data);
	}
	
	
	function aprshow(){
		$data = array();
		$act = $this->input->post('act');
		$key = 'index_apr_remark';
		if ($act=='set'){
			$arr = array(
				'apr0' => $this->input->post('apr0'),
				'apr1' => $this->input->post('apr1'),
				'apr3' => $this->input->post('apr3'),
				'apr6' => $this->input->post('apr6'),
				'apr9' => $this->input->post('apr9'),
				'apr12' => $this->input->post('apr12'),
				'apr18' => $this->input->post('apr18'),
				'apr24' => $this->input->post('apr24'),
				'apr36' => $this->input->post('apr36'),
			
				'award0' => $this->input->post('award0'),
				'award1' => $this->input->post('award1'),
				'award3' => $this->input->post('award3'),
				'award6' => $this->input->post('award6'),
				'award9' => $this->input->post('award9'),
				'award12' => $this->input->post('award12'),
				'award18' => $this->input->post('award18'),
				'award24' => $this->input->post('award24'),
				'award36' => $this->input->post('award36'),
			
				'shouyi0' => $this->input->post('shouyi0'),
				'shouyi1' => $this->input->post('shouyi1'),
				'shouyi3' => $this->input->post('shouyi3'),
				'shouyi6' => $this->input->post('shouyi6'),
				'shouyi9' => $this->input->post('shouyi9'),
				'shouyi12' => $this->input->post('shouyi12'),
				'shouyi18' => $this->input->post('shouyi18'),
				'shouyi24' => $this->input->post('shouyi24'),
				'shouyi36' => $this->input->post('shouyi36'),
			);
			$this->mycache->set_array($key,$arr);
			echo "<script>alert('保存成功');</script>";
			exit;
		}
		$arr = $this->mycache->get_array($key);
		$data['arr'] = $arr;
		$data['title'] = "利率明细设置";
		
		$data['tab'] = $this->tab;
		$data['sub'] = 'interest';
		
		$this->load->view('/sys_admin/borrow/borrow_aprshow', $data);
	}
	
	function tomorrow(){
		$data = array();
		$act = $this->input->post('act');
		$key = 'index_borrow_tomorrow';
		if ($act=='set'){
			$arr = array(
				'bt_date' => $this->input->post('bt_date'),
				'r1t1' => $this->input->post('r1t1'),
				'r1t2' => $this->input->post('r1t2'),
				'r1t3' => $this->input->post('r1t3'),
				'r2t1' => $this->input->post('r2t1'),
				'r2t2' => $this->input->post('r2t2'),
				'r2t3' => $this->input->post('r2t3'),
				'r3t1' => $this->input->post('r3t1'),
				'r3t2' => $this->input->post('r3t2'),
				'r3t3' => $this->input->post('r3t3'),
				'r4t1' => $this->input->post('r4t1'),
				'r4t2' => $this->input->post('r4t2'),
				'r4t3' => $this->input->post('r4t3'),
			);
			$this->mycache->set_array($key,$arr);
			echo "<script>alert('保存成功');</script>";
			exit;
		}
		$arr = $this->mycache->get_array($key);
		$data['arr'] = $arr;
		$data['title'] = "发标预告";
		
		$data['tab'] = $this->tab;
		$data['sub'] = 'trailer';
		
		$this->load->view('/sys_admin/borrow/borrow_tomorrow', $data);
	}
	
	function borrow_check(){
		$this->load->model('Borrow_model');
		$id = $this->input->post('id');
		
		$admin_user = $this->session->userdata('admin_user');
		
		if($this->input->post('fshow') == 'again'){
			$is_check = $this->input->post('is_check');
			$data['is_check'] = $is_check;
			$data["check_time"]		= time();
			$data["check_user"]		= $admin_user['user_id'];
			$log = '复查';
		}else{
			$data['is_verify'] = $this->input->post('is_verify');
			$data["verify_user"]		= $admin_user['user_id'];
			$data["verify_time"]		= time();
			$log = '初查';	
		}
		
		$data["verify_remark"]		= $this->input->post('verify_remark');
				
		$falg = $this->Borrow_model->update($id, $data);
		
		$this->load->model('log_model');
		
		$username = $admin_user['username'];
		
		$this->log_model->add_log('[发标待审数据检查][借款标][ID=='.$id.'] ['.$username.'已'.$log.']');
		
		echo $falg;
	}
	
	//前台申请标列表
	function applylist(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$data = array();
		$this->load->model('Borrow_model');
		$vdata = $this->Borrow_model->get_borrow_apply_list($data,(int)$per_page);
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/borrow/applylist/?';
		$config['total_rows'] = $vdata['count'];
		$config['per_page'] = sys_admin_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'bapply';
		$vdata['borrow_arr'] 		= $this->Borrow_model->arrays('borrow_type');
		
		$this->load->view('/sys_admin/borrow/borrow_apply', $vdata);
		
	}
	
	

	
	
	private function post_data(){
		
		$ret = array(
			'msg' => '',
			'url' => '',
		);
		// by liub 20150202 新增产品类型数据
		$ftype = intval($this->input->post('ftype'));
		
		// 通用信息
		$data = array(
			'ftype'         => intval($this->input->post('ftype')),//产品类型
			'title'      => trim($this->input->post('title')),// 产品名称
			'fuse'  => trim($this->input->post('fuse')),// 资金用途
			'style'         => $this->input->post('style'),//还款方式
			'account'         => $this->input->post('account'),//融资金额
			'apr'         => $this->input->post('apr'),// 年化利率
			'auto_apr'         => $this->input->post('auto_apr'),//自动投标比例
			'fsafe'         	=> $this->input->post('fsafe'),// 标的安全等级
			'lowest_account'         => $this->input->post('lowest_account'),//最低投标金额
			'most_account'         => $this->input->post('most_account'),// 最大投标金额
			'award'         => $this->input->post('award'),//奖励
			'funds'         => $this->input->post('funds'),//奖励比例/现金
			'each_time'		=>strtotime($this->input->post('each_time')),// 最后还款日
			'bill_starttime'		=>strtotime($this->input->post('bill_starttime')),//标的起始时间
			'bill_times'		=>intval($this->input->post('bill_times')),// 投资周期
			'work_time'		=>intval($this->input->post('work_time')),//计息方式
			'works_hours' 		=>intval($this->input->post('works_hours')),//审核时间
			'time_limit'       	=> $this->input->post("time_limit")?$this->input->post("time_limit"):0,//标的期限
			'is_day'     		 => intval($this->input->post('is_day')),//是否天标
			'time_limit_day' =>0,		// 天标天数
			'delay_repay'     		 => intval($this->input->post('delay_repay')),// T+N还款
		
			// 红本新增
			'user_id' 			=>intval($this->input->post('brw_userid')),		// 借款人ID
			'loan_userid'     	 => intval($this->input->post('brw_userid_loan')),// 担保人ID
			'late_days' 		=>intval($this->input->post('late_days')),		// 逾期日
			'late_apr'     		 => $this->input->post('late_apr'),// 逾期利率
		
			'valid_time'      	=> $this->input->post("valid_time"),
			'bill_endtime'		=>strtotime($this->input->post('bill_endtime')),//
		);
		
		if(0== $ftype){
			$ret['msg'] = '标的类型不能为空';
		}
		
		if(!isset($data['user_id']) || empty($data['user_id'])){
			$ret['msg'] = '借款人用户ID不能为空';
		}
			
		if(!isset($data['loan_userid'])  || empty($data['loan_userid'])){
			$ret['msg'] = '担保人用户ID不能为空';
		}
		$this->load->model('User_model');
		$user_obj = $this->User_model->get_by_uid($data['user_id']);
		if(!isset($user_obj) || empty($user_obj)){
			$ret['msg'] = '借款人用户ID不存在';
		}
			
		$loan_user_obj = $this->User_model->get_by_uid($data['loan_userid']);
		if(!isset($loan_user_obj) || empty($loan_user_obj)){
			$ret['msg'] = '担保人用户ID不存在';
		}
		
		// 标的起始时间 
		$bill_starttime_post 			= $this->input->post("bill_starttime");
		if(isset($bill_starttime_post) && $bill_starttime_post){
			$min = date('i',strtotime($bill_starttime_post)); 
			if($min%10 == 0){
				$bill_starttime = strtotime($bill_starttime_post);
			}else{
				$m = round($min/10)*10;
				$new_time = date('Y-m-d H',strtotime($bill_starttime_post));
				$bill_starttime = strtotime($new_time.':'.$m);
			}
			$data['bill_starttime'] = $bill_starttime;
		}else{
			$ret['msg'] = '标的起始时间不能为空';
		}
		
		// 标的正常最后还款时间
		$each_time_post 			= $this->input->post("each_time");
		if(isset($each_time_post) && $each_time_post){
			$min = date('i',strtotime($each_time_post)); 
			if($min%10 == 0){
				$each_time = strtotime($each_time_post);
			}else{
				$m = round($min/10)*10;
				$new_time = date('Y-m-d H',strtotime($each_time_post));
				$each_time = strtotime($new_time.':'.$m);
			}
			$data['each_time'] = $each_time;
		}else{
			$ret['msg'] = '标的正常还款时间不能为空';
		}
		
		// 预约发标时间
		$auto_verify_time_post 			= $this->input->post("auto_verify_time");
		if(isset($auto_verify_time_post) && $auto_verify_time_post){
			$min = date('i',strtotime($auto_verify_time_post)); 
			if($min%10 == 0){
				$auto_verify_time = strtotime($auto_verify_time_post);
			}else{
				$m = round($min/10)*10;
				$new_time = date('Y-m-d H',strtotime($auto_verify_time_post));
				$auto_verify_time = strtotime($new_time.':'.$m);
			}
			$data['auto_verify_time'] = $auto_verify_time;
		}else{
			$ret['msg'] = '预约发标时间不能为空';
		}
		
		//标的背景调查审核--starting;
		$thumbnailstr = $this->input->post('thumbnailstr');
		$temp = array();
		foreach ($thumbnailstr as $k){
			$carr = explode('|', $k);
			$ad['id'] = $carr[0];
			$ad['pic'] = $carr[1];
			$ad['fileformat'] = $carr[2];
			$ad['valid'] = $carr[3];
			$ad['content'] = $carr[4];
			$temp[] = $ad;
		}
		$data['fsubject'] = serialize($temp);
		//背景调查审核--ending;
		
		// 天标只允许 一次性还款
		if($data['is_day'] == 1){
			$data['style'] = 3;
		}
		// 条件排除
		
		if(!($data['account'] < 10000000 && $data['account'] > 500)){
			$ret['msg'] = '金额必须大于500且小于1000w';
		}else
		if($data['account'] % 50 !=0){
			$ret['msg'] = '金额必须 为50的倍数';
		}elseif($data['apr'] < 0 || $data['apr'] > 30){
			$ret['msg'] = '利率必须在0%-30%之间';
		}elseif($data['style'] == '' || $data['style'] == 0){
			$ret['msg'] = '还款方式不能为空';
		}else if($data['auto_verify_time'] == ''){
			$ret['msg'] = '标的预约发标时间不能为空';
		}else if($data['bill_starttime'] == ''){
			$ret['msg'] = '标的起始时间不能为空';
		}else if($data['bill_starttime'] < $data['auto_verify_time']){
			$ret['msg'] = '标的起始时间不能小于预约发标时间';
		}
		
		/**else if($data['fsafe'] == ''){
			$ret['msg'] = '标的安全等级不能为空';
		}*/
		
		else if($data['fuse'] == ''){
			$ret['msg'] = '资金用途不能为空';
		}else if($data['lowest_account'] == ''){
			$ret['msg'] = '最小投资金额不能为空';
		}else if($data['lowest_account']%50 != 0){
			$ret['msg'] = '最小投资金额必须为50的倍数';
		}else if($data['ftype']==''){
			$ret['msg'] = '标的产品不能为空';
		}
		// ****** by liub 20141214 投资有效期 = 标的起始时间 - 预约发标时间
		$data['valid_time'] = floor(($data['bill_starttime'] - $data['auto_verify_time'])/(3600*24));
		
		if(intval($this->input->post('is_day')) == 1){
			$data['time_limit_day'] = intval($this->input->post("time_limit_day"));
		}

		// 条件判断,截止程序
		if($ret['msg']){
			echo json_encode($ret);
			exit;
		}
		
		// 标的总还利息计算
  		$idata['time_limit'] 		= $data['time_limit'];
  		$idata['style'] 			= $data['style'];
		$idata['account'] 			= $data['account'];
		$idata['apr'] 				= $data['apr'];
		
		// 利率奖励
		if($data['award'] == 1){
			$idata['apr'] 				+= $data['funds'];
		}else{
		
		}//----------- end
		
		$idata['is_day'] 			= $data['is_day'];
		$idata['time_limit_day'] 	= $data['time_limit_day'];
		$idata['type'] 				= "all";
		$idata['borrow_time'] 		= $data['bill_starttime'];//标的启标时间,计算每期还款时间
		
		// 发标计算总还利息
  		$interest_result = equal_interest($idata);
  		$data['repayment_account'] 	= $interest_result['repayment_account'];//总还金额
  		$data['monthly_repayment'] 	= $interest_result['monthly_repayment'];//每月需还金额
  		
  		if($idata[is_day] == 1 && $idata[time_limit] == 1){
  			$data['monthly_repayment'] 	= $interest_result['repayment_account'];	
  		}

		$data['update_time'] 		= time();
		
				
		// 图片处理
		
		$data = self::upload_images($data);
		
		// 相异数据
		if(0== $ftype){
			$ret['msg'] = '标的类型不能为空';
			echo json_encode($ret);exit;
		}else{// 其他类型标的数据信息
			$data =  self::hongData($data);
		}

		return  $data ;
		
	}
	
	function upload_images($data){
		$file_upload  =  "f_file";
		$thumbnail = Array();
  		$thumbnail = $this->upload_borrow_thumbnail(UPFILES_BORROW_DEAL,$file_upload);
  		
  		if(count($thumbnail) != 0 ){
			$file1 = "";
			$file2 = "";
			$file3 = "";
			$file4 = "";
			$file5 = "";
			$file6 = "";
			$file7 = "";
			$file8 = "";
			$file9 = "";
			$file10= "";
			$file11 = "";
			$file12 = "";
			$file13 = "";
			$file14 = "";
			$file15 = "";
  			$file1  = $thumbnail[0]== null || $thumbnail[0] == '' ? 0 : $thumbnail[0];
			$file2  = $thumbnail[1]== null || $thumbnail[1] == '' ? 0 : $thumbnail[1];
			$file3  = $thumbnail[2]== null || $thumbnail[2] == '' ? 0 : $thumbnail[2];
			$file4  = $thumbnail[3]== null || $thumbnail[3] == '' ? 0 : $thumbnail[3];
			$file5  = $thumbnail[4]== null || $thumbnail[4] == '' ? 0 : $thumbnail[4];
			$file6  = $thumbnail[5]== null || $thumbnail[5] == '' ? 0 : $thumbnail[5];
			$file7  = $thumbnail[6]== null || $thumbnail[6] == '' ? 0 : $thumbnail[6];
			$file8  = $thumbnail[7]== null || $thumbnail[7] == '' ? 0 : $thumbnail[7];
			$file9  = $thumbnail[8]== null || $thumbnail[8] == '' ? 0 : $thumbnail[8];
			$file10 = $thumbnail[9]== null || $thumbnail[9] == '' ? 0 : $thumbnail[9];
			$file11 = $thumbnail[10]== null || $thumbnail[10] == '' ? 0 : $thumbnail[10];
			$file12 = $thumbnail[11]== null || $thumbnail[11] == '' ? 0 : $thumbnail[11];
			$file13 = $thumbnail[12]== null || $thumbnail[12] == '' ? 0 : $thumbnail[12];
			$file14 = $thumbnail[13]== null || $thumbnail[13] == '' ? 0 : $thumbnail[13];
			$file15 = $thumbnail[14]== null || $thumbnail[14] == '' ? 0 : $thumbnail[14];
			$data['bill_edition'] = $file1.','.$file2.','.$file3.','.$file4.','.$file5.','.$file6.','.$file7.','.$file8.','.$file9.','.$file10.','.$file11.','.$file12.','.$file13.','.$file14.','.$file15;
  		}
  		
  		return $data;
	
	}
	
	// 特殊数据
	private function piaoData($data){
		
		$data['bill_unit' ]= trim($this->input->post('bill_unit'));//鉴定单位
		$data['bill_identification' ]= trim($this->input->post('bill_identification'));//真伪鉴定
		$data['bill_good' ]= trim($this->input->post('bill_good'));//完整性
		$data['bill_clarity' ]= trim($this->input->post('bill_clarity'));//字体清晰度
		$data['bill_compliance' ]= trim($this->input->post('bill_compliance'));//用章合规性
		$data['bill_bank' ]=intval($this->input->post('bill_bank'));//承兑银行
		$data['security_mode' ]=trim($this->input->post('security_mode'));//保障方式
		$data['summary' ]=trim($this->input->post('summary'));// 产品亮点
  		
  		return $data;
	
	}
	
	//红本特殊数据
	private function hongData($data){
		$data['bill_unit' ]= trim($this->input->post('hong_bill_unit'));//借款描述
		$data['bill_identification' ]= trim($this->input->post('hong_bill_identification'));//借款人信息
		$data['bill_clarity' ]= trim($this->input->post('hong_bill_clarity'));//还款来源
		$data['security_mode' ]=trim($this->input->post('hong_security_mode'));//抵押详情：
		$data['summary' ]=trim($this->input->post('hong_summary'));// 产品亮点
		
  		return $data;
	}
	
	
	
	// 上传文件并打水印
	private function upload_borrow_thumbnail($path,$file_upload){
		if(!file_exists($path)){
			mkdir($path,0777);
		}
		
		$config['upload_path'] = $path;
		$config['allowed_types'] = 'jpg|png|doc|docx|wps';
		$config['file_name'] = 'borrow_'.uniqid();
		$config['overwrite'] = true;
		$config['max_size'] = '512';
		$config['max_width']  = '2048';
		$config['max_height']  = '3000';
		$this->load->library('upload', $config);
		$data = $this->upload->multiple($file_upload);
		$name = Array();
		$i = 0;
		
		foreach ($data['files'] as $value) {
			$thumbnail = $path.$value['file_name'];
			// 添加水印				
			$this->load->library('image_lib');		
            $warter['source_image'] = $thumbnail;
			$warter['wm_type'] = 'overlay';
			//******  by liub  20141211 修改水印图片，以及水印位置为 底部居右
			$warter['wm_overlay_path'] = WM_OVERLAY_FILE_PNG;
			$warter['wm_opacity'] = '60';
			$warter['wm_vrt_alignment'] = 'B';// 底部
			$warter['wm_hor_alignment'] = 'R';// 右边
			// ****************** end 
			$this->image_lib->initialize($warter);
			$this->image_lib->watermark();
			$name[$i] = $thumbnail;
			$i++;
		}
		
		return $name;
	}
	
	function borrow_type_list(){
		$this->load->model('Borrow_model');
		$list = $this->Borrow_model->get_borrow_type('type');
		$vdata['list']=$list;
		$this->load->view('/sys_admin/borrow/borrow_type_list', $vdata);
	}

	function borrow_type($id){
		$this->load->model('Borrow_model');
		$act = $this->input->post('act');
		$meg['msg'] = '';
		if($act == 'update'){
			$id = $this->input->post('id');
			$name = $this->input->post('name');
			$singtel = $this->input->post('singtel');
			if(trim($name) == '') $meg['msg'] = "名称不能为空";
			elseif(trim($singtel) == '') $meg['msg'] = "简称不能为空";
			else {
				$data = array('name'=>$name,'singtel'=>$singtel);
				$meg['msg'] = $this->Borrow_model->update_borrow_type($data,array('id'=>$id));
				//$meg['msg'] = '1';
			}
			echo json_encode($meg);
			exit;
		}
		 $borr_type = $this->Borrow_model->get_borrow_type('type',array('id'=>$id));
		 $vdata['borr_type'] = $borr_type[0];
		$this->load->view('/sys_admin/borrow/borrow_type', $vdata);
	}
}
=======
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Borrow extends MY_Controller {
    	
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /sys_admin/login');
			exit;
		}
		
		$this->tab = 'borrowtender';
	}	
	
	/**
	 * 
	 * 计算投资周期
	 */
	function billtimes(){
		$ret = array(
			'msg' => '',
			'info' => '',
		);
		if(intval($_POST['billtimeday'])>0){
			$billtimes=intval($_POST['billtimeday']);
			$time=strtotime($_POST['bill_starttime'])+$billtimes*24*3600;
			//****** 20141222 by liub 
			$repaymenttimes=date('Y-m-d H:i',$time);
			$ret['retinfo'] = $repaymenttimes;
			$ret['info'] = $billtimes;
		}else{
			// 以自然月计算最后还款日期
			$billtimes=intval($_POST['billtime']);
			$time = strtotime($_POST['bill_starttime']);
			$repaymenttimes=date('Y-m-d H:i',$time);
			$repay_time = strtotime($repaymenttimes);
 			$last_repay_time = get_times(array("time"=>$repay_time,"num"=>$billtimes,"type"=>'month'));
 			$last_repay_date=date('Y-m-d H:i',$last_repay_time);
			$ret['retinfo'] = $last_repay_date;
			$ret['info'] = $billtimes*30;
		}		
		echo json_encode($ret);
		exit;
	}
	
	
	/**
	 * 
	 * 公共的标列表数据
	 * @param int $fstatus 
	 * @param varchar $sub 
	 * @param int $show
	 * @param varchar $func
	 */
	private function common_borrow_list($fstatus,$sub,$show,$func){
    	$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
    	$title 				= $this->input->get('title');
    	$work_time 			= $this->input->get('work_time');
    	$style 				= $this->input->get('style');
    	$search_fstatus 	= $this->input->get('fstatus')?$this->input->get('fstatus'):-2;
		
		if($title != ''){
			$data['where']['title'] = $title;
		}
		if($work_time != 0){
			$data['where']['work_time'] = $work_time;
		}
		if($style != 0){
			$data['where']['style'] = $style;
		}
		$data['page_size']  = 10;
		$data['page_start'] = (int)$per_page;
		if($fstatus == 'full'){
			$data['where']['qtype'] = $fstatus;
		}else if($fstatus == 'passing'){
			$data['where']['qtype'] = $fstatus;
		}else if($fstatus == 'refunding'){
			$data['where']['qtype'] = $fstatus;
		}else{
			if($search_fstatus != -2){
				$data['where']['fstatus'] = $search_fstatus;
			}else{
				if($fstatus > 0){
					$data['where']['fstatus'] = $fstatus;
				}	
			}
		}
		$vdata = $this->common_borrow_conset();	
		$data['select'] 	= 1;
		$this->load->model('Borrow_model');
		$borrow_list = $this->Borrow_model->get_borrow_user_list($data);
		$vdata['borrow_list'] = $borrow_list['borrow_list'];
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/borrow/'.$func.'/?fstatus='.$search_fstatus.'&title='.$title.'&work_time='.$work_time.'&style='.$style;
		$config['total_rows'] = $borrow_list['borrow_count'];
		$config['per_page'] = sys_admin_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
						
		$vdata['tab'] = $this->tab;		
    	$vdata['sub'] = $sub;
		$vdata['show'] = $show;
		$vdata['fstatus'] = $search_fstatus;
		$vdata['title'] = $title;
		$vdata['work_time'] = $work_time;
		$vdata['style'] = $style;
		$vdata['func'] = $func;
		$this->load->view('/sys_admin/borrow/borrow_list', $vdata);		
    }
    
    
    /**
     * 
     * 共用的常量
     */
    private function common_borrow_conset(){
    	$this->load->model('Borrow_model');
    	$vdata['borrow_type'] 				= $this->Borrow_model->arrays('');//标产品
		$vdata['borrow_work_time'] 			= $this->Borrow_model->arrays('borrow_work_time');//计息方式
		$vdata['borrow_style'] 				= $this->Borrow_model->arrays('borrow_style');//还款方式
		$vdata['borrow_type'] 				= $this->Borrow_model->arrays('borrow_type');//还款方式
		$vdata['borrow_status'] 			= $this->Borrow_model->arrays('borrow_status');//状态
		$vdata['borrow_award'] 				= $this->Borrow_model->arrays('borrow_award');//客户奖励
		$vdata['borrow_good'] 				= $this->Borrow_model->arrays('borrow_good');
		$vdata['borrow_bill_bank'] 			= $this->Borrow_model->arrays('borrow_bill_bank');
		$vdata['borrow_time_limit'] 		= $this->Borrow_model->arrays('borrow_time_limit');//借款期限
		$vdata['borrow_valid_time'] 		= $this->Borrow_model->arrays('borrow_valid_time');//投资有效期
		$vdata['borrow_day_time_limit'] 	= $this->Borrow_model->arrays('borrow_day_time_limit');//借款期限
		$vdata['borrow_verify'] 			= $this->Borrow_model->arrays('borrow_verify');//是否初检
		$vdata['borrow_check'] 				= $this->Borrow_model->arrays('borrow_check');//是否复检
		
		$this->load->model('Conset_model');
		//标的安全等级
		$vdata['borrow_fsafe'] 				= $this->Conset_model->get_credit_title_list();	
		//标的资金用途
		$vdata['borrow_fuse'] 				= $this->Conset_model->get_use_title_list();	
		//背景调查审核
		$vdata['borrow_fsubject'] 			= $this->Conset_model->get_subject_title_list();
		
		$vdata['conset_subject_valid'] = $this->Conset_model->arrays('conset_subject_valid');
		$vdata['conset_subject_file'] = $this->Conset_model->arrays('conset_subject_file');
		return $vdata;
		
    }
    
	private function common_borrow_obj($borrow_id,$sub){
		$data['select'] = 1;
		$this->load->model('Borrow_model');
		$borrow = $this->Borrow_model->get_borrow_obj($borrow_id, $data);
		$vdata = $this->common_borrow_conset();
		$vdata['borrow'] 					= $borrow;
		$this->load->model('Conset_model');
		if($borrow['fsubject']){
			$vdata['list'] = unserialize($borrow['fsubject']);
			$tmp = array();
			foreach($vdata['list'] as $v){
				$tmp[] =  $v['id'];
			}
			$vdata['sublist'] = $tmp;
		}
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = $sub;
		$vdata['fshow'] = $sub;
		return $vdata;
	}
	
	
	/***/
	private function upload_borrow_all($path,$file_upload){
		if(!file_exists($path)){
			mkdir($path,0777);
		}
		$config['upload_path'] = $path;
		$config['allowed_types'] = 'jpg|png|doc|docx|wps';
		$config['file_name'] = 'borrow_'.uniqid();
		$config['overwrite'] = true;
		$config['max_size'] = '512';
		$config['max_width']  = '2048';
		$config['max_height']  = '3000';
		$this->load->library('upload', $config);
		$thumbnail = Array();
		for($i = 0; $i < 2;$i++){
			if($this->upload->do_upload($file_upload[$i])){
				$tmp = $this->upload->data();
				//echo 
				$thumbnail[$i] = $path.$config['file_name'].$tmp['file_ext'];
				// 添加水印				
				$this->load->library('image_lib');		
	            $warter['source_image'] = $thumbnail[$i];
				$warter['wm_type'] = 'overlay';
				
				//******  by liub  20141211 修改水印图片，以及水印位置为 底部居右
				$warter['wm_overlay_path'] = WM_OVERLAY_FILE_PNG;
				$warter['wm_opacity'] = '60';
				$warter['wm_vrt_alignment'] = 'B';// 底部
				$warter['wm_hor_alignment'] = 'R';// 右边
				// ****************** end 
				
				$this->image_lib->initialize($warter);
				$this->image_lib->watermark();
			}else{
				echo "12345____".$i;exit;
			}
		}
		return $thumbnail;
	}
	
	
	public function upload_view(){
		$this->load->model('Conset_model');
		$vdata['conset_subject_valid'] = $this->Conset_model->arrays('conset_subject_valid');
		$vdata['conset_subject_file'] = $this->Conset_model->arrays('conset_subject_file');
		
		$vdata['sid'] = $_POST['sid'];
		$response_html = $this->load->view('/sys_admin/borrow/borrow_upload',$vdata,true);
		echo $response_html;
	}
	
	public function doupload(){
		$ret = array(
			'msg' => '',
			'url' => ''
		);
		
		$file_format = $_POST['fformat'];
		
		$sid =  $_POST['sid'];
		$this->load->model('Conset_model');
		$conset_subject_file = $this->Conset_model->arrays('conset_subject_file');
		
		
		$thumbnail = "";
		if(isset($_FILES['thumbnail']) && $_FILES['thumbnail']['name'] != ''){
			
			$thumbnail = $this->attach(UPFILES_CONSET_SUBJECT_IMG,$_FILES['thumbnail']['tmp_name'],$_FILES['thumbnail']['name'],$file_format );
			if ($thumbnail){
				$fileinfo = '';
				$thumbnailstr = '';
				
				$content=$_POST['subjectcon'];
				$valid=$_POST['subjectvalid'];
				
				$thumbnailstr .= $sid.'|'.$thumbnail.'|'.$file_format.'|'.$valid.'|'.$content;
				
				if($file_format == 1){
					$fileinfo .= '<li><a target="_blank" href="'.CONSET_SUBJECT_IMG.$thumbnail.'"><img src="'.CONSET_SUBJECT_IMG.$thumbnail.'" width="60" height="60"  /></a><br />'.$thumbnail.'<br />'.$conset_subject_file[$file_format].'<br />审核资料有效期:'.$valid.'个月<br /><p>资料提交说明：'.$content.'</p><input type="hidden" name="thumbnailstr[]" value="'.$thumbnailstr.'"/></li>';
				}else{
					$fileinfo .= '<li>'.$thumbnail.'<br />'.$conset_subject_file[$file_format].'<br />审核资料有效期:'.$valid.'个月<br /><p>资料提交说明：'.$content.'</p><input type="hidden" name="thumbnailstr[]" value="'.$thumbnailstr.'"/></li>';
				}				
					
				$ret['msg'] = array('thumbnail'=>$thumbnailstr,'info'=>$fileinfo);
				echo json_encode($ret);
				exit;
		
			}else{
				$ret['url'] = 'error';
				$ret['msg'] = '保存失败';
			}
		}else{
			$ret['url'] = 'error';
			$ret['msg'] = $this->upload->display_errors();
		}
		echo json_encode($ret);
		exit;
		
		
	}
	
	
	/**
	 * 添加标的标视图
	 */
	
	function zhs_publish_borrow(){
		$vdata = $this->common_borrow_conset();
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'zhs_publish_borrow';
		$this->load->view('/sys_admin/borrow/publish_borrow', $vdata);
	}
	
	/**
	 * 添加标的标
	 */
	function add(){

		// 前端页面获取数据
		$data = $this->post_data();

		//$data['user_id'] = ADMIN_ID; //平台自发	
			
  		$this->load->model('Borrow_model');
		$borrow_id = $this->Borrow_model->add($data);
		if($borrow_id == 0){
			$ret['msg'] = "发标失败。";
			echo json_encode($ret);exit;
		}
		$ret['msg'] = $borrow_id;
		$this->load->model('log_model');$this->log_model->add_log('[添加][标的标][id=='.$borrow_id.']');
		echo json_encode($ret);
	}
	
	/**
	 * 编辑标的标视图
	 * @param int $borrow_id
	 */
	function edit_view($borrow_id){
		$vdata = $this->common_borrow_obj($borrow_id, 'first');
		$this->load->view('/sys_admin/borrow/borrow_edit', $vdata);
	}
	
	/**
	 * 编辑标的标
	 */
	function edit(){
		$id = intval($this->input->post('id'));
		
		$data = $this->post_data();
  		$this->load->model('Borrow_model');
		$falg = $this->Borrow_model->update($id, $data);
		
		$ret['msg'] = $falg;
		$this->load->model('log_model');$this->log_model->add_log('[修改][标的标][ID=='.$id.']');
		echo json_encode($ret);
	}
	
	
	/**
	 * 标列表
	 */
	function zhs_borrowlist(){
		$this->common_borrow_list(-1,'all','all', 'zhs_borrowlist');	
	}
	
	private function borrow_verify_data($sub,$func){
    	$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
    	$title 				= $this->input->get('title');
    	$work_time 				= $this->input->get('work_time');
    	$style 				= $this->input->get('style');
    	$search_fstatus 			= $this->input->get('fstatus')?$this->input->get('fstatus'):-2;
		if($title != ''){
			$data['where']['title'] = $title;
		}
		if($work_time != 0){
			$data['where']['work_time'] = $work_time;
		}
		if($style != 0){
			$data['where']['style'] = $style;
		}
		$data['page_size']  = sys_admin_LIST_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		if($sub == 'first'){
			$data['where']['qtype'] = $sub;
		}else if($sub == 'again'){
			$data['where']['qtype'] = $sub;
		}else if($sub == 'publish'){
			$data['where']['qtype'] = $sub;
		}
		
		$vdata = $this->common_borrow_conset();	
		
		$data['select'] 	= 1;
		$this->load->model('Borrow_model');
		$borrow_list = $this->Borrow_model->get_borrow_user_list($data);
		$vdata['borrow_list'] = $borrow_list['borrow_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/borrow/'.$func.'/?';
		$config['total_rows'] = $borrow_list['borrow_count'];
		$config['per_page'] = sys_admin_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
									
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = $sub;
		$vdata['fshow'] = $sub;
		
		$this->load->view('/sys_admin/borrow/borrow_verify_list', $vdata);
	}
	
	/**
	 * 
	 *待复检中投标产品列表
	 */
	function zhs_borrow_verify_again_list(){
		$this->borrow_verify_data('again','zhs_borrow_verify_again_list');	
	}
	
	 /**
	 * 
	 * 待初检中的投标产品列表
	 */
    function zhs_borrow_verify_list(){
    	$this->borrow_verify_data('first','zhs_borrow_verify_list');			
    }
    
	 /**
	 * 
	 * 待发布的投标产品列表
	 */
    function zhs_borrow_publish_list(){
    	$this->borrow_verify_data('publish','zhs_borrow_publish_list');			
    }
	    
     /**
	 * 
	 * 借款中的投标产品列表
	 */
    function zhs_borrow_going_list(){
    	$this->common_borrow_list(1, 'now','going','zhs_borrow_going_list');    	
    }
    
    /**
	 * 
	 * 满标待审标产品列表
	 */
    function zhs_borrow_full_list(){
    	$this->common_borrow_list('full','full','full', 'zhs_borrow_full_list');
    }
    
	/**
	 * 
	 * 还款中标产品列表
	 */
    function zhs_borrow_repayment_list(){    	
    	$this->common_borrow_list('refunding','repayment','repayment','zhs_borrow_repayment_list');
    }
    
    /**
	 * 
	 * 过往标产品列表
	 */
    function zhs_borrow_passing_list(){    	
    	$this->common_borrow_list('passing','passing','passing','zhs_borrow_passing_list');
    }
    
    
	/**
	 * 
	 * 投标详细记录
	 */
	function borrow_view($borrow_id){
		$this->load->model('Borrow_model');
		$this->load->model('User_model');
		
		$vdata = $this->common_borrow_obj($borrow_id, 'all');
		
		//投标记录信息
		$borrow_tender_list 			= $this->Borrow_model->get_borrow_tender(array('select'=>1,'where'=>array('borrow_id'=>$borrow_id)));
		$borrow_tender_list1 = array();
		foreach ($borrow_tender_list as $tender){
			$user_obj = $this->User_model->get_by_uid($tender['user_id']);
			$tender['username'] = $user_obj['username'];
			$tender['realname'] = $user_obj['realname'];
			$borrow_tender_list1[] 		= $tender;
		}
		$vdata['borrow_tender_list'] = $borrow_tender_list1;			
	
		$this->load->view('/sys_admin/borrow/borrow_view', $vdata);
	}
	
	/**
	 * 撤标
	 */
	function withdraw($borrow_id){
		//接收参数
		$data = array('fstatus'=>2);
		$this->load->model('Borrow_model');
		$borrow_obj = $this->Borrow_model->get_borrow_obj($borrow_id, $data);
		
		$user = $this->session->userdata('rsh_user');
		$data['id'] 		= $borrow_id;
		$data['user_id'] 	= $borrow_obj['user_id'];
		$data['ip'] 		= $this->js->get_ip();
		$this->load->model('log_model');$this->log_model->add_log('[撤标][借款标]');
		echo $this->Borrow_model->Cancel($data);
	}
	
	
	
	
	function repayment(){
		//接收参数
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$strtime			= $this->input->get('strtime');
		$endtime			= $this->input->get('endtime');
		
		$data = array();		
		
		$data['where']['fstatus'] = 0;
		
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		
		$data['page_size']  = sys_admin_LIST_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$data['select'] 	= 1;
		$this->load->model('User_model');
		$this->load->model('Borrow_model');
		
		$vdata = $this->common_borrow_conset();
		$repayment_list  = $this->Borrow_model->get_repayment_list($data);
		
		$vdata['borrow_list'] = $repayment_list['repayment_list'];
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/borrow/repayment/?';
		$config['total_rows'] = $vdata['repayment_count'];
		$config['per_page'] = sys_admin_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		$vdata['strtime'] 	= $strtime;
		$vdata['endtime'] 	= $endtime;
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'repayment';
		$vdata['show'] = 'repayment';
		$this->load->view('/sys_admin/borrow/borrow_list', $vdata);
	}
	
	function repaymentexcel(){
		$title 				= $this->input->get('title');
		$strtime			= $this->input->get('strtime');
		$endtime			= $this->input->get('endtime');
		
		$data = array();
		/**/
		//TODO ***********************************
		if($fstatus != -1){
			$data['where']['fstatus'] = $fstatus;
		}
		if($title != ''){
			$data['where']['title'] = $title;
		}
		
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		
		$data['select'] 	= 1;
		$this->load->model('Borrow_model');
		$this->load->model('User_model');
		$vdata = $this->Borrow_model->get_repayment_list($data);
		$repayment_list = $vdata['repayment_list'];
		$repayment_list_new = array();
		foreach($repayment_list as $repayment){
			$user_obj = $this->User_model->get_by_uid($repayment['user_id']);
			$repayment['username'] = $user_obj['username'];
			$repayment['repayment_account_majia'] = $this->Borrow_model->get_borrow_collection_majia(array('borrow_id'=>$repayment['borrow_id'],'forder'=>$repayment['forder']));
			$repayment_list_new[] = $repayment;
		}
		$vdata['repayment_list'] = $repayment_list_new;
		
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory'); 
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle("还款列表")->setDescription(COMPANY_NAME); 
		$objPHPExcel->setActiveSheetIndex(0);
		$titles = array("编号", "借款户", "借款标题", "期数", "到期时间", "还款金额", "本金", "利息","真实金额", "马甲金额", "还款时间");
		
		for($i = 0; $i < count($titles); $i ++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
		}
		$repayment_list = $vdata['repayment_list'];
		for($i = 0; $i < count($repayment_list); $i++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $repayment_list[$i]['id']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $repayment_list[$i]['username']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, $repayment_list[$i]['title']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, ($repayment_list[$i]['forder']+1).'/'.$repayment_list[$i]['time_limit']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, date('Y-m-d H:i:s', $repayment_list[$i]['repayment_time']));
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i+2, $repayment_list[$i]['repayment_account']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $i+2, $repayment_list[$i]['repayment_account']-$repayment_list[$i]['repayment_account_majia']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $i+2, $repayment_list[$i]['repayment_account_majia']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8, $i+2, $repayment_list[$i]['capital'].' ');
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(9, $i+2, $repayment_list[$i]['interest']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(10, $i+2, date('Y-m-d H:i:s', $repayment_list[$i]['repayment_yestime']));
		}
		
		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
		//发送标题强制用户下载文件 
		header('Content-Type: application/vnd.ms-excel'); 
		header('Content-Disposition: attachment;filename="还款列表.xls"'); 
		header('Cache-Control: max-age=0'); 
		$objWriter->save('php://output'); 
	}
	
	/**
	 * 初检
	 */
	function verify_view($borrow_id){
		$vdata = $this->common_borrow_obj($borrow_id, 'first');
		$this->load->view('/sys_admin/borrow/borrow_verify', $vdata);
	}
	/**
	 * 复检
	 */
	function verify_again_view($borrow_id){
		$vdata = $this->common_borrow_obj($borrow_id, 'again');
		$this->load->view('/sys_admin/borrow/borrow_verify', $vdata);
	}
	

	/**
	 * 审标
	 */
	function verify(){
		$id 			= $this->input->post('id');
		$fstatus 		= $this->input->post('fstatus');
		$verify_remark 	= $this->input->post('verify_remark');
		
		$this->load->model('Borrow_model');
  		$borrow = $this->Borrow_model->get_borrow_obj($id);
  		if($borrow['fstatus'] != 0){
  			echo "该标不处于待审核状态";
  			return;
  		}
  		
  		$data["fstatus"]			= $fstatus;
  		$data["verify_remark"]		= $verify_remark;
  		$data["verify_time"]		= time();
  		
  		$admin_user = $this->session->userdata('admin_user');
  		$data["verify_user"]		= $admin_user['user_id'];
  		
		$flag = $this->Borrow_model->verify($id, $data);
		
		$this->log->write_log('info', $flag);
		if($flag == 1 && $fstatus == 1){//自动投标
			$this->Borrow_model->auto_tender_borrow($id);
		}
		$this->load->model('log_model');$this->log_model->add_log('[审核][发标]');
		echo $flag;
	}
	
	/**
	 * 进入复审
	 */
	function review_view($borrow_id){
		$vdata = $this->common_borrow_obj($borrow_id, 'full');
		$this->load->view('/sys_admin/borrow/borrow_review', $vdata);
	}
	
	
	/**
	 * 确认复审
	 */
	function review(){
		$id 				= $this->input->post('id');
		$fstatus 			= $this->input->post('fstatus');
		$repayment_remark 	= $this->input->post('repayment_remark');

  		$data['id']					= $id;
  		$data['ip']					= $this->js->get_ip();
  		$data["fstatus"]			= $fstatus;
  		$data["repayment_remark"]	= $repayment_remark;
  		
  		// TODO by liub 20150203
  		// $data["repayment_user"]		= ADMIN_ID;// 还款人
  		$this->load->model('Borrow_model');
  		$flag = 0;
  		if($fstatus == 3){//满标复审通过
  			$flag = $this->Borrow_model->zhs_borrow_review_true($data);
  		}elseif ($fstatus == 4){//复审不通过
  			//$flag = $this->Borrow_model->borrow_review_false($data);
  		}
  		$this->load->model('log_model');
  		$this->log_model->add_log('[复审][满标]');
  		
		echo $flag;
	}
	
	
	
	
	function repay($repayment_id, $user_id){
		$data['repayment_id'] 	= $repayment_id;
		$data['user_id'] 		= $user_id;
		$data['ip']	 			= $this->js->get_ip();
		$this->load->model('Borrow_model');
		echo $this->Borrow_model->repay($data);
		
	}
	
	/**
	 * 回款排行
	 */
	function repayment_top(){
		$strtime 			= $this->input->get('strtime');
		$endtime 			= $this->input->get('endtime');
		$username 			= $this->input->get('username');
		$data['where']['strtime']	= $strtime;
		$data['where']['endtime']	= $endtime;
		$data['where']['username']	= $username;
		$this->load->model('Borrow_model');
		$vdata['repayment_list'] = $this->Borrow_model->repayment_top($data);
		$vdata['strtime'] = $strtime;
		$vdata['endtime'] = $endtime;
		$vdata['username'] = $username;
		$this->load->view('/sys_admin/borrow/repayment_top', $vdata);
	}
	
	/**
	 * 网站统计
	 */
	function index(){
		$this->load->model('Borrow_model');		
		$data['total_data'] = $this->Borrow_model->admin_total_index();
		
		$data['tab'] = $this->tab;
		$data['sub'] = 'month';
		
		$this->load->view('/sys_admin/borrow/borrow_index', $data);
	}
	
	private function tender_data($limit=false){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$username 			= $this->input->get('username');
		$is_self 	= isset($_GET['is_self']) ? $this->input->get('is_self') : -1;
		$fstatus = isset($_GET['fstatus']) ? $this->input->get('fstatus') : -1;
		$strtime 			= $this->input->get('strtime');
		$endtime 			= $this->input->get('endtime');
		
		$title 			= $this->input->get('title');
		$time_limit 			= $this->input->get('time_limit');
		
		if($title != ''){
			$data['where']['title'] = $title;
		}
		
		if($time_limit != '' && $time_limit != -1){
			$data['where']['time_limit'] = $time_limit;
		}
		
		if($username != ''){
			$data['where']['username'] = $username;
		}
		
		if($is_self != '' && $is_self != -1){
			$data['where']['is_self'] = $is_self;
		}
		
		if($fstatus != '' && $fstatus != -1){
			$data['where']['fstatus'] = $fstatus;
		}
		
		
		$data['where']['strtime']	= $strtime;
		$data['where']['endtime']	= $endtime;
		$data['page_size']  = sys_admin_LIST_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$data['limit'] = $limit;
		
		$this->load->model('Borrow_model');
		$vdata 			  = $this->Borrow_model->borrow_tender_user_list($data);
		$tender_list = $vdata['tender_list'];
		$tender_user_id = array();
		foreach ($tender_list as $tender){
			$first_tender_obj = $this->Borrow_model->user_first_tender($tender['user_id']);
			if($first_tender_obj['id'] == $tender['id']){
				$tender['flag'] = 1;
			}			
			$tender_list1[] = $tender;
		}
		$vdata['tender_list'] = $tender_list1;
		$vdata['user_self'] 	= $this->Borrow_model->arrays("user_self");
		$vdata['borrow_tender_status'] 	= $this->Borrow_model->arrays("borrow_tender_status");
		$vdata['borrow_time_limit'] 	= $this->Borrow_model->arrays("borrow_time_limit");
		
		$vdata['username'] = $username;
		$vdata['is_self'] 	= $is_self;
		$vdata['fstatus'] = $fstatus;
		$vdata['strtime'] = $strtime;
		$vdata['endtime'] = $endtime;
		$vdata['title'] = $title;
		$vdata['time_limit'] = $time_limit;
		
		$vdata['user_count'] = $vdata['tender_user_count'];
		$vdata['tender_money'] =$vdata['tender_money_count'];
		
		return $vdata;
	}
	
	function tenderlist(){
		
		$vdata = $this->tender_data();
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/borrow/tenderlist/?strtime='.$vdata['strtime'].'&endtime='.$vdata['endtime'].'&username='.$vdata['username'].'&fstatus='.$vdata['fstatus'];
		$config['total_rows'] = $vdata['tender_count'];
		$config['per_page'] = sys_admin_LIST_PAGE_SIZE;
		$this->pagination->initialize($config);
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'tender';
		
		$this->load->view('/sys_admin/borrow/tender_list', $vdata);
	}
	
	/**
	 * 流转标撤销投标
	 */
	function cancel_tender(){
		$tid = intval($this->input->post('tid'));
		$this->load->model('Borrow_model');
		$msg = $this->Borrow_model->tender_cancel($tid);
		echo $msg;
		exit;
	}
	
	function tenderlistexcel(){
		
		$vdata = $this->tender_data('all');
		
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory'); 
		
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle("投标记录")->setDescription(COMPANY_NAME); 
		
		$objPHPExcel->setActiveSheetIndex(0);
		$titles = array("用户编号", "投资客", "借款标", "投标金额", "期限", "投标时间");
		
		for($i = 0; $i < count($titles); $i ++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
		}
				
		$tender_list = $vdata['tender_list'];
		
		for($i = 0; $i < count($tender_list); $i++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $tender_list[$i]['id']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $tender_list[$i]['username']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, $tender_list[$i]['title']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, number_format($tender_list[$i]['account'],2));
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, $tender_list[$i]['time_limit']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i+2, date('Y-m-d H:i:s', $tender_list[$i]['add_time']));
		}
		
		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
		header('Content-Type: application/vnd.ms-excel'); 
		header('Content-Disposition: attachment;filename="投标记录.xls"'); 
		header('Cache-Control: max-age=0'); 
		$objWriter->save('php://output'); 
		exit;
	}
	
	
	function borrow_account(){
		$this->load->model('Borrow_model');
		$post_data = $this->input->post();
		$id = intval($post_data['id']);
		$lowest = intval($post_data['lowest_account']);
		$most = intval($post_data['most_account']);
		$borrow_info = $this->Borrow_model->get_borrow_obj($id);
		if($lowest<100){
			echo "最低投标金额不能小于100元";
			exit;
		}else if($most != 0 && $lowest>$most){
			echo "最低投标金额不能大于最高投标金额";
			exit;
		}else if($most != 0 && $most>$borrow_info['account']){
			echo "最高投标金额不能大于借贷总金额";
			exit;
		}
		
		$ret = $this->Borrow_model->update($id,array('lowest_account'=>$lowest,'most_account'=>$most));
		
		echo $ret ? '操作成功' : '操作失败';
	}
	
	
	
	function borrow_single($id){
		$this->load->model('Borrow_model');
		$data['id']	= $id;
  		$data['ip']	= $this->js->get_ip();
		echo $this->Borrow_model->borrow_review_single_false($data);
	}
	
	
	function aprshow(){
		$data = array();
		$act = $this->input->post('act');
		$key = 'index_apr_remark';
		if ($act=='set'){
			$arr = array(
				'apr0' => $this->input->post('apr0'),
				'apr1' => $this->input->post('apr1'),
				'apr3' => $this->input->post('apr3'),
				'apr6' => $this->input->post('apr6'),
				'apr9' => $this->input->post('apr9'),
				'apr12' => $this->input->post('apr12'),
				'apr18' => $this->input->post('apr18'),
				'apr24' => $this->input->post('apr24'),
				'apr36' => $this->input->post('apr36'),
			
				'award0' => $this->input->post('award0'),
				'award1' => $this->input->post('award1'),
				'award3' => $this->input->post('award3'),
				'award6' => $this->input->post('award6'),
				'award9' => $this->input->post('award9'),
				'award12' => $this->input->post('award12'),
				'award18' => $this->input->post('award18'),
				'award24' => $this->input->post('award24'),
				'award36' => $this->input->post('award36'),
			
				'shouyi0' => $this->input->post('shouyi0'),
				'shouyi1' => $this->input->post('shouyi1'),
				'shouyi3' => $this->input->post('shouyi3'),
				'shouyi6' => $this->input->post('shouyi6'),
				'shouyi9' => $this->input->post('shouyi9'),
				'shouyi12' => $this->input->post('shouyi12'),
				'shouyi18' => $this->input->post('shouyi18'),
				'shouyi24' => $this->input->post('shouyi24'),
				'shouyi36' => $this->input->post('shouyi36'),
			);
			$this->mycache->set_array($key,$arr);
			echo "<script>alert('保存成功');</script>";
			exit;
		}
		$arr = $this->mycache->get_array($key);
		$data['arr'] = $arr;
		$data['title'] = "利率明细设置";
		
		$data['tab'] = $this->tab;
		$data['sub'] = 'interest';
		
		$this->load->view('/sys_admin/borrow/borrow_aprshow', $data);
	}
	
	function tomorrow(){
		$data = array();
		$act = $this->input->post('act');
		$key = 'index_borrow_tomorrow';
		if ($act=='set'){
			$arr = array(
				'bt_date' => $this->input->post('bt_date'),
				'r1t1' => $this->input->post('r1t1'),
				'r1t2' => $this->input->post('r1t2'),
				'r1t3' => $this->input->post('r1t3'),
				'r2t1' => $this->input->post('r2t1'),
				'r2t2' => $this->input->post('r2t2'),
				'r2t3' => $this->input->post('r2t3'),
				'r3t1' => $this->input->post('r3t1'),
				'r3t2' => $this->input->post('r3t2'),
				'r3t3' => $this->input->post('r3t3'),
				'r4t1' => $this->input->post('r4t1'),
				'r4t2' => $this->input->post('r4t2'),
				'r4t3' => $this->input->post('r4t3'),
			);
			$this->mycache->set_array($key,$arr);
			echo "<script>alert('保存成功');</script>";
			exit;
		}
		$arr = $this->mycache->get_array($key);
		$data['arr'] = $arr;
		$data['title'] = "发标预告";
		
		$data['tab'] = $this->tab;
		$data['sub'] = 'trailer';
		
		$this->load->view('/sys_admin/borrow/borrow_tomorrow', $data);
	}
	
	function borrow_check(){
		$this->load->model('Borrow_model');
		$id = $this->input->post('id');
		
		$admin_user = $this->session->userdata('admin_user');
		
		if($this->input->post('fshow') == 'again'){
			$is_check = $this->input->post('is_check');
			$data['is_check'] = $is_check;
			$data["check_time"]		= time();
			$data["check_user"]		= $admin_user['user_id'];
			$log = '复查';
		}else{
			$data['is_verify'] = $this->input->post('is_verify');
			$data["verify_user"]		= $admin_user['user_id'];
			$data["verify_time"]		= time();
			$log = '初查';	
		}
		
		$data["verify_remark"]		= $this->input->post('verify_remark');
				
		$falg = $this->Borrow_model->update($id, $data);
		
		$this->load->model('log_model');
		
		$username = $admin_user['username'];
		
		$this->log_model->add_log('[发标待审数据检查][借款标][ID=='.$id.'] ['.$username.'已'.$log.']');
		
		echo $falg;
	}
	
	//前台申请标列表
	function applylist(){
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$data = array();
		$this->load->model('Borrow_model');
		$vdata = $this->Borrow_model->get_borrow_apply_list($data,(int)$per_page);
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/borrow/applylist/?';
		$config['total_rows'] = $vdata['count'];
		$config['per_page'] = sys_admin_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'bapply';
		$vdata['borrow_arr'] 		= $this->Borrow_model->arrays('borrow_type');
		
		$this->load->view('/sys_admin/borrow/borrow_apply', $vdata);
		
	}
	
	

	
	
	private function post_data(){
		
		$ret = array(
			'msg' => '',
			'url' => '',
		);
		// by liub 20150202 新增产品类型数据
		$ftype = intval($this->input->post('ftype'));
		
		// 通用信息
		$data = array(
			'ftype'         => intval($this->input->post('ftype')),//产品类型
			'title'      => trim($this->input->post('title')),// 产品名称
			'fuse'  => trim($this->input->post('fuse')),// 资金用途
			'style'         => $this->input->post('style'),//还款方式
			'account'         => $this->input->post('account'),//融资金额
			'apr'         => $this->input->post('apr'),// 年化利率
			'auto_apr'         => $this->input->post('auto_apr'),//自动投标比例
			'fsafe'         	=> $this->input->post('fsafe'),// 标的安全等级
			'lowest_account'         => $this->input->post('lowest_account'),//最低投标金额
			'most_account'         => $this->input->post('most_account'),// 最大投标金额
			'award'         => $this->input->post('award'),//奖励
			'funds'         => $this->input->post('funds'),//奖励比例/现金
			'each_time'		=>strtotime($this->input->post('each_time')),// 最后还款日
			'bill_starttime'		=>strtotime($this->input->post('bill_starttime')),//标的起始时间
			'bill_times'		=>intval($this->input->post('bill_times')),// 投资周期
			'work_time'		=>intval($this->input->post('work_time')),//计息方式
			'works_hours' 		=>intval($this->input->post('works_hours')),//审核时间
			'time_limit'       	=> $this->input->post("time_limit")?$this->input->post("time_limit"):0,//标的期限
			'is_day'     		 => intval($this->input->post('is_day')),//是否天标
			'time_limit_day' =>0,		// 天标天数
			'delay_repay'     		 => intval($this->input->post('delay_repay')),// T+N还款
		
			// 红本新增
			'user_id' 			=>intval($this->input->post('brw_userid')),		// 借款人ID
			'loan_userid'     	 => intval($this->input->post('brw_userid_loan')),// 担保人ID
			'late_days' 		=>intval($this->input->post('late_days')),		// 逾期日
			'late_apr'     		 => $this->input->post('late_apr'),// 逾期利率
		
			'valid_time'      	=> $this->input->post("valid_time"),
			'bill_endtime'		=>strtotime($this->input->post('bill_endtime')),//
		);
		
		if(0== $ftype){
			$ret['msg'] = '标的类型不能为空';
		}
		
		if(!isset($data['user_id']) || empty($data['user_id'])){
			$ret['msg'] = '借款人用户ID不能为空';
		}
			
		if(!isset($data['loan_userid'])  || empty($data['loan_userid'])){
			$ret['msg'] = '担保人用户ID不能为空';
		}
		$this->load->model('User_model');
		$user_obj = $this->User_model->get_by_uid($data['user_id']);
		if(!isset($user_obj) || empty($user_obj)){
			$ret['msg'] = '借款人用户ID不存在';
		}
			
		$loan_user_obj = $this->User_model->get_by_uid($data['loan_userid']);
		if(!isset($loan_user_obj) || empty($loan_user_obj)){
			$ret['msg'] = '担保人用户ID不存在';
		}
		
		// 标的起始时间 
		$bill_starttime_post 			= $this->input->post("bill_starttime");
		if(isset($bill_starttime_post) && $bill_starttime_post){
			$min = date('i',strtotime($bill_starttime_post)); 
			if($min%10 == 0){
				$bill_starttime = strtotime($bill_starttime_post);
			}else{
				$m = round($min/10)*10;
				$new_time = date('Y-m-d H',strtotime($bill_starttime_post));
				$bill_starttime = strtotime($new_time.':'.$m);
			}
			$data['bill_starttime'] = $bill_starttime;
		}else{
			$ret['msg'] = '标的起始时间不能为空';
		}
		
		// 标的正常最后还款时间
		$each_time_post 			= $this->input->post("each_time");
		if(isset($each_time_post) && $each_time_post){
			$min = date('i',strtotime($each_time_post)); 
			if($min%10 == 0){
				$each_time = strtotime($each_time_post);
			}else{
				$m = round($min/10)*10;
				$new_time = date('Y-m-d H',strtotime($each_time_post));
				$each_time = strtotime($new_time.':'.$m);
			}
			$data['each_time'] = $each_time;
		}else{
			$ret['msg'] = '标的正常还款时间不能为空';
		}
		
		// 预约发标时间
		$auto_verify_time_post 			= $this->input->post("auto_verify_time");
		if(isset($auto_verify_time_post) && $auto_verify_time_post){
			$min = date('i',strtotime($auto_verify_time_post)); 
			if($min%10 == 0){
				$auto_verify_time = strtotime($auto_verify_time_post);
			}else{
				$m = round($min/10)*10;
				$new_time = date('Y-m-d H',strtotime($auto_verify_time_post));
				$auto_verify_time = strtotime($new_time.':'.$m);
			}
			$data['auto_verify_time'] = $auto_verify_time;
		}else{
			$ret['msg'] = '预约发标时间不能为空';
		}
		
		//标的背景调查审核--starting;
		$thumbnailstr = $this->input->post('thumbnailstr');
		$temp = array();
		foreach ($thumbnailstr as $k){
			$carr = explode('|', $k);
			$ad['id'] = $carr[0];
			$ad['pic'] = $carr[1];
			$ad['fileformat'] = $carr[2];
			$ad['valid'] = $carr[3];
			$ad['content'] = $carr[4];
			$temp[] = $ad;
		}
		$data['fsubject'] = serialize($temp);
		//背景调查审核--ending;
		
		// 天标只允许 一次性还款
		if($data['is_day'] == 1){
			$data['style'] = 3;
		}
		// 条件排除
		
		if(!($data['account'] < 10000000 && $data['account'] > 500)){
			$ret['msg'] = '金额必须大于500且小于1000w';
		}else
		if($data['account'] % 50 !=0){
			$ret['msg'] = '金额必须 为50的倍数';
		}elseif($data['apr'] < 0 || $data['apr'] > 30){
			$ret['msg'] = '利率必须在0%-30%之间';
		}elseif($data['style'] == '' || $data['style'] == 0){
			$ret['msg'] = '还款方式不能为空';
		}else if($data['auto_verify_time'] == ''){
			$ret['msg'] = '标的预约发标时间不能为空';
		}else if($data['bill_starttime'] == ''){
			$ret['msg'] = '标的起始时间不能为空';
		}else if($data['bill_starttime'] < $data['auto_verify_time']){
			$ret['msg'] = '标的起始时间不能小于预约发标时间';
		}
		
		/**else if($data['fsafe'] == ''){
			$ret['msg'] = '标的安全等级不能为空';
		}*/
		
		else if($data['fuse'] == ''){
			$ret['msg'] = '资金用途不能为空';
		}else if($data['lowest_account'] == ''){
			$ret['msg'] = '最小投资金额不能为空';
		}else if($data['lowest_account']%50 != 0){
			$ret['msg'] = '最小投资金额必须为50的倍数';
		}else if($data['ftype']==''){
			$ret['msg'] = '标的产品不能为空';
		}
		// ****** by liub 20141214 投资有效期 = 标的起始时间 - 预约发标时间
		$data['valid_time'] = floor(($data['bill_starttime'] - $data['auto_verify_time'])/(3600*24));
		
		if(intval($this->input->post('is_day')) == 1){
			$data['time_limit_day'] = intval($this->input->post("time_limit_day"));
		}

		// 条件判断,截止程序
		if($ret['msg']){
			echo json_encode($ret);
			exit;
		}
		
		// 标的总还利息计算
  		$idata['time_limit'] 		= $data['time_limit'];
  		$idata['style'] 			= $data['style'];
		$idata['account'] 			= $data['account'];
		$idata['apr'] 				= $data['apr'];
		
		// 利率奖励
		if($data['award'] == 1){
			$idata['apr'] 				+= $data['funds'];
		}else{
		
		}//----------- end
		
		$idata['is_day'] 			= $data['is_day'];
		$idata['time_limit_day'] 	= $data['time_limit_day'];
		$idata['type'] 				= "all";
		$idata['borrow_time'] 		= $data['bill_starttime'];//标的启标时间,计算每期还款时间
		
		// 发标计算总还利息
  		$interest_result = equal_interest($idata);
  		$data['repayment_account'] 	= $interest_result['repayment_account'];//总还金额
  		$data['monthly_repayment'] 	= $interest_result['monthly_repayment'];//每月需还金额
  		
  		if($idata[is_day] == 1 && $idata[time_limit] == 1){
  			$data['monthly_repayment'] 	= $interest_result['repayment_account'];	
  		}

		$data['update_time'] 		= time();
		
				
		// 图片处理
		
		$data = self::upload_images($data);
		
		// 相异数据
		if(0== $ftype){
			$ret['msg'] = '标的类型不能为空';
			echo json_encode($ret);exit;
		}else{// 其他类型标的数据信息
			$data =  self::hongData($data);
		}

		return  $data ;
		
	}
	
	function upload_images($data){
		$file_upload  =  "f_file";
		$thumbnail = Array();
  		$thumbnail = $this->upload_borrow_thumbnail(UPFILES_BORROW_DEAL,$file_upload);
  		
  		if(count($thumbnail) != 0 ){
			$file1 = "";
			$file2 = "";
			$file3 = "";
			$file4 = "";
			$file5 = "";
			$file6 = "";
			$file7 = "";
			$file8 = "";
			$file9 = "";
			$file10= "";
  			$file11 = "";
			$file12 = "";
			$file13 = "";
			$file14 = "";
			$file15 = "";
			$file1   = $thumbnail[0]== null || $thumbnail[0] == '' ? 0 : $thumbnail[0];
			$file2   = $thumbnail[1]== null || $thumbnail[1] == '' ? 0 : $thumbnail[1];
			$file3   = $thumbnail[2]== null || $thumbnail[2] == '' ? 0 : $thumbnail[2];
			$file4   = $thumbnail[3]== null || $thumbnail[3] == '' ? 0 : $thumbnail[3];
			$file5   = $thumbnail[4]== null || $thumbnail[4] == '' ? 0 : $thumbnail[4];
			$file6   = $thumbnail[5]== null || $thumbnail[5] == '' ? 0 : $thumbnail[5];
			$file7   = $thumbnail[6]== null || $thumbnail[6] == '' ? 0 : $thumbnail[6];
			$file8   = $thumbnail[7]== null || $thumbnail[7] == '' ? 0 : $thumbnail[7];
			$file9   = $thumbnail[8]== null || $thumbnail[8] == '' ? 0 : $thumbnail[8];
			$file10  = $thumbnail[9]== null || $thumbnail[9] == '' ? 0 : $thumbnail[9];
			$file11  = $thumbnail[10]== null || $thumbnail[10] == '' ? 0 : $thumbnail[10];
			$file12  = $thumbnail[11]== null || $thumbnail[11] == '' ? 0 : $thumbnail[11];
			$file13  = $thumbnail[12]== null || $thumbnail[12] == '' ? 0 : $thumbnail[12];
			$file14  = $thumbnail[13]== null || $thumbnail[13] == '' ? 0 : $thumbnail[13];
			$file15  = $thumbnail[14]== null || $thumbnail[14] == '' ? 0 : $thumbnail[14];
			$data['bill_edition'] = $file1.','.$file2.','.$file3.','.$file4.','.$file5.','.$file6.','.$file7.','.$file8.','.$file9.','.$file10.','.$file11.','.$file12.','.$file13.','.$file14.','.$file15;
  		}
  		
  		return $data;
	
	}
	
	// 特殊数据
	private function piaoData($data){
		
		$data['bill_unit' ]= trim($this->input->post('bill_unit'));//鉴定单位
		$data['bill_identification' ]= trim($this->input->post('bill_identification'));//真伪鉴定
		$data['bill_good' ]= trim($this->input->post('bill_good'));//完整性
		$data['bill_clarity' ]= trim($this->input->post('bill_clarity'));//字体清晰度
		$data['bill_compliance' ]= trim($this->input->post('bill_compliance'));//用章合规性
		$data['bill_bank' ]=intval($this->input->post('bill_bank'));//承兑银行
		$data['security_mode' ]=trim($this->input->post('security_mode'));//保障方式
		$data['summary' ]=trim($this->input->post('summary'));// 产品亮点
  		
  		return $data;
	
	}
	
	//红本特殊数据
	private function hongData($data){
		$data['bill_unit' ]= trim($this->input->post('hong_bill_unit'));//借款描述
		$data['bill_identification' ]= trim($this->input->post('hong_bill_identification'));//借款人信息
		$data['bill_clarity' ]= trim($this->input->post('hong_bill_clarity'));//还款来源
		$data['security_mode' ]=trim($this->input->post('hong_security_mode'));//抵押详情：
		$data['summary' ]=trim($this->input->post('hong_summary'));// 产品亮点
		
  		return $data;
	}
	
	
	
	// 上传文件并打水印
	private function upload_borrow_thumbnail($path,$file_upload){
		if(!file_exists($path)){
			mkdir($path,0777);
		}
		
		$config['upload_path'] = $path;
		$config['allowed_types'] = 'jpg|png|doc|docx|wps';
		$config['file_name'] = 'borrow_'.uniqid();
		$config['overwrite'] = true;
		$config['max_size'] = '512';
		$config['max_width']  = '2048';
		$config['max_height']  = '3000';
		$this->load->library('upload', $config);
		$data = $this->upload->multiple($file_upload);
		$name = Array();
		$i = 0;
		
		foreach ($data['files'] as $value) {
			$thumbnail = $path.$value['file_name'];
			// 添加水印				
			$this->load->library('image_lib');		
            $warter['source_image'] = $thumbnail;
			$warter['wm_type'] = 'overlay';
			//******  by liub  20141211 修改水印图片，以及水印位置为 底部居右
			$warter['wm_overlay_path'] = WM_OVERLAY_FILE_PNG;
			$warter['wm_opacity'] = '60';
			$warter['wm_vrt_alignment'] = 'B';// 底部
			$warter['wm_hor_alignment'] = 'R';// 右边
			// ****************** end 
			$this->image_lib->initialize($warter);
			$this->image_lib->watermark();
			$name[$i] = $thumbnail;
			$i++;
		}
		
		return $name;
	}

	/*
	*test @zhu
	*time 20150713
	*/
	function borrow_test(){
		//$this->load->model('user_reward_model');
		//$this->user_reward_model->jiaoben_test();
		//echo 'test page!';
		//$res=$this->db->query('select * from rsh_borrow ');
		//$res=$this->db->query('update rsh_borrow set title="配资标34" where id=34');
		//$res=$this->db->query("insert into rsh_payment(name,nid,fstatus,config,description,payment_order)values('丰付','fengfu',0,'','',1)");
		//echo $this->db->query_affected_rows();
		//$res=$this->db->query('select * from rsh_borrow where id=48 limit ');
		/*foreach($res->result() as $row){
			var_dump($row);
			echo '<br><hr>';	
			//echo date('Y-m-d H:i:s',$row->each_time);
			//echo '<br><hr>';
		}*/
	}
	
	function borrow_type_list(){
		$this->load->model('Borrow_model');
		$list = $this->Borrow_model->get_borrow_type('type');
		$vdata['list']=$list;
		$this->load->view('/sys_admin/borrow/borrow_type_list', $vdata);
	}
	function borrow_type($id){
		$this->load->model('Borrow_model');
		$act = $this->input->post('act');
		$meg['msg'] = '';
		if($act == 'update'){
			$id = $this->input->post('id');
			$name = $this->input->post('name');
			$singtel = $this->input->post('singtel');
			if(trim($name) == '') $meg['msg'] = "名称不能为空";
			elseif(trim($singtel) == '') $meg['msg'] = "简称不能为空";
			else {
				$data = array('name'=>$name,'singtel'=>$singtel);
				$meg['msg'] = $this->Borrow_model->update_borrow_type($data,array('id'=>$id));
				//$meg['msg'] = '1';
			}
			echo json_encode($meg);
			exit;
		}
		 $borr_type = $this->Borrow_model->get_borrow_type('type',array('id'=>$id));
		 $vdata['borr_type'] = $borr_type[0];
		$this->load->view('/sys_admin/borrow/borrow_type', $vdata);
	}
}
>>>>>>> d47623b610d79a4eeb489500830c00d70933f414
